package com.atguigu.gmall.ware.controller;

import com.atguigu.gmall.ware.bean.WareInfo;
import com.atguigu.gmall.ware.bean.WareSku;
import com.atguigu.gmall.ware.mapper.WareInfoMapper;
import com.atguigu.gmall.ware.mapper.WareSkuMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * WareController
 *
 * @author yangchao
 * @version 开发阶段
 * @description TODO
 * @date 2022/3/27 21:22
 */
@RestController
@RequestMapping("/ware")
public class WareController {
    @Autowired
    private WareInfoMapper wareInfoMapper;
    @Autowired
    private WareSkuMapper wareSkuMapper;
    //8 查询仓库信息集合
    @GetMapping("getWareInfoList")
    public  List<WareInfo>  getWareInfoList() {
        List<WareInfo> list = wareInfoMapper.selectList(null);
        return list;
    }
    @PostMapping("saveWare")
    public void saveWare(@RequestBody WareInfo wareInfo) {
        wareInfoMapper.insert(wareInfo );
    }

    @PostMapping("updateWare")
    public void updateWare(@RequestBody WareInfo wareInfo) {
        wareInfoMapper.updateById(wareInfo);
    }


    @DeleteMapping("deteleWare")
    public void deteleWare(@RequestBody WareInfo wareInfo) {
        wareInfoMapper.deleteById(wareInfo.getId());
    }

    //8 查询仓库信息集合
    @GetMapping("WareSkuList")
    public  List<WareSku>  WareSkuList() {
        List<WareSku> wareSkuList = wareSkuMapper.selectList(null);
        wareSkuList.forEach(e->{
            WareInfo wareInfo = wareInfoMapper.selectById(e.getWarehouseId());
            e.setWarehouseName(wareInfo.getName());
        });
        return wareSkuList;
    }
//    @PostMapping("saveWare")
//    public void saveWare(@RequestBody WareInfo wareInfo) {
//        wareInfoMapper.insert(wareInfo );
//    }

    @PostMapping("updateWareSku")
    public void updateWareSku(@RequestBody Map<String, Object> map) {

        WareSku wareSku1 = wareSkuMapper.selectById((String) map.get("id"));

        WareSku wareSku = new WareSku();
        wareSku.setId((String)map.get("id"));
        String remaining = (String) map.get("remaining");
        Integer remain = Integer.parseInt(remaining);
        wareSku.setStock(remain+wareSku1.getStock());
        wareSkuMapper.updateById(wareSku);
    }
//
//
//    @DeleteMapping("deteleWare")
//    public void deteleWare(@RequestBody WareInfo wareInfo) {
//        wareInfoMapper.deleteById(wareInfo.getId());
//    }


}
